package org.firecloud.bluetooth;

import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.firecloud.nuedc2015.app.R;

import java.io.IOException;
import java.util.*;

/**
 * Created by fc on 15-9-1.
 */
public class BlueToothSelectDeviceDialog extends AlertDialog {
    private ListView listViewDievices;
    private Button buttonRefresh;
    private List<Map<String,Object>> listViewData;
    private SimpleAdapter listViewAdapter;
    private BluetoothAdapter bluetoothAdapter;
    private BlueToothSelectDeviceDialogCallBack callBack;
    public BlueToothSelectDeviceDialog(Context context,BluetoothAdapter bluetoothAdapter, BlueToothSelectDeviceDialogCallBack callBack){
        super(context);
        this.show();
        this.callBack = callBack;
        this.bluetoothAdapter = bluetoothAdapter;
        this.setContentView(R.layout.org_firecloud_bluetooth_dialog_view);
        listViewDievices = (ListView) this.findViewById(R.id.org_firecloud_bluetooth_lv_devices);
        buttonRefresh = (Button) this.findViewById(R.id.org_firecloud_bluetooth_btn_refresh);
        listViewData = new ArrayList<Map<String, Object>>();
        listViewAdapter = new SimpleAdapter(context,listViewData,
                R.layout.org_firecloud_bluetooth_dialog_view_list_row,
                new String[]{"Name","Mac"},
                new int[]{R.id.org_firecloud_bluetooth_list_row_name,R.id.org_firecloud_bluetooth_list_row_mac});
        Set<BluetoothDevice> bluetoothDevices = bluetoothAdapter.getBondedDevices();
        for(Iterator iterator = bluetoothDevices.iterator();iterator.hasNext();)
        {
            Map<String,Object> map = new HashMap<String,Object>();
            BluetoothDevice device = (BluetoothDevice)iterator.next();
            map.put("Name",device.getName());
            map.put("Mac",device.getAddress());
            map.put("Device",device);
            listViewData.add(map);
        }
        listViewDievices.setAdapter(listViewAdapter);
        listViewDievices.setOnItemClickListener(new ListView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                BluetoothDevice device = (BluetoothDevice)listViewData.get(i).get("Device");
                try {
                    BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
                    socket.connect();
                    dismiss();
                    BlueToothSelectDeviceDialog.this.callBack.BlueToothSelectDeviceDialogLinkOK(socket);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}